package com.example.experiment05.mapper;

import com.example.experiment05.dto.UserAddress03;
import com.example.experiment05.entity.Address;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: YancyZhang
 * @time: 2022/6/20 16:19
 */
public class UserAddress03RowMapper implements ResultSetExtractor<UserAddress03> {
    @Override
    public UserAddress03 extractData(ResultSet rs) throws SQLException, DataAccessException {
//        DateTimeFormatter formatter = DateTimeFormatterUtils.getFormatter();
        List<Address> addresses = new ArrayList<>();
        UserAddress03.UserAddress03Builder builder = UserAddress03.builder();
        while (rs.next()) {
            builder.Id(rs.getLong("user_id"));
            builder.name(rs.getString("name"));
            builder.createTime(rs.getDate("create_time"));
            Address a = Address.builder().id(rs.getLong("id"))
                    .detail(rs.getString("detail"))
                    .userId(rs.getLong("user_id"))
                    .createTime(rs.getDate("a.create_time"))
                    .build();
            addresses.add(a);
        }
        return builder.addresses(addresses).build();
    }
}